var AREA = '';
$(function () {
    // 获取默认地址
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.myAddress();
    if(!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    }else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    result.data.area ? AREA = result.data.area : AREA = '北京市';
    if(result.data.area) {
        $('#showPicker').html(result.data.area);
    }
    $('#input-name').val(result.data.receiver);
    $('#input-phone').val(result.data.phone);
    $('#input-address').val(result.data.address);
    $('#input-code').val(result.data.postalcode);
    loading.hide();
});

// 提交信息按钮
$(document).on('click', '#showTooltips', function () {
    var info = {
        name: $.trim($('#input-name').val()),
        phone: $.trim($('#input-phone').val()),
        address: $.trim($('#input-address').val()),
        code: $.trim($('#input-code').val()),
        area: $.trim($('#showPicker').html())
    };
    for (i in info) {
        if (info[i] == '') {
            weui.alert('信息不得为空');
            return false;
        }
    }
    var result = Model.user.modelifyAddress({
        receiver: info.name,
        phone: info.phone,
        area: info.area,
        address: info.address,
        postalcode: info.code
    });
    if (result.code) {
        Common.fn.formatOutput(result);
        return false;
    }
    weui.alert('信息修改成功！', function () {
        location.reload;
    });
});

// 选择地区下拉菜单
$(document).on('click', '#showPicker', function () {
    weui.picker(Config.province, {
        className: 'custom-classname',
        container: 'body',
        defaultValue: [AREA],
        onChange: function (result) {
            console.log(result)
        },
        onConfirm: function (result) {
            $('#showPicker').html(result);
        },
        id: 'singleLinePicker'
    });
});